package framework.view.components.scrollbar
{
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;

public class ScrollBarBg extends Sprite
{
    //==========================================================================
    //  Class constants
    //==========================================================================
    protected static const TOP_ARROW:String = "topArrow";
    protected static const BOTTOM_ARROW:String = "bottomArrow";
    protected static const BG:String = "bg";
    protected static const SYMBOL:String = "symbol";
    //==========================================================================
    //  Constructor
    //==========================================================================
    public function ScrollBarBg()
    {
        super();
        init();
    }
    //==========================================================================
    //  Variables
    //==========================================================================
    protected var bgAsset:DisplayObjectContainer;

    protected var topArrow:DisplayObject;
    protected var bottomArrow:DisplayObject;
    protected var bg:DisplayObject;
    //==========================================================================
    //  Properties
    //==========================================================================
    public function set skin(value:DisplayObjectContainer):void
    {
        if (bgAsset && this.contains(bgAsset))
        {
            this.removeChild(bgAsset);
            bgAsset = null;
        }
        bgAsset = value;
        this.addChild(bgAsset);
        setSkin();
    }
	
	public function get skin():DisplayObjectContainer
	{
		return bgAsset;
	}
    //==========================================================================
    //  Overridden methods: ScollBarThumbAsset
    //==========================================================================
    override public function set height(value:Number):void
    {
        if (value < (this.topArrow.height + this.bottomArrow.height))
        {
            value = this.topArrow.height + this.bottomArrow.height;
        }
        this.bg.height = value - this.topArrow.height - this.bottomArrow.height;
        this.bg.y = this.topArrow.y + this.topArrow.height;
        this.bottomArrow.y = (this.bg.y * 100 + this.bg.height * 100) / 100;
    }
    //==========================================================================
    //  Protected methods
    //==========================================================================
    protected function init():void
    {
		var sp:Sprite = new Sprite();
		
		var spTop:Sprite = new Sprite();
		spTop.graphics.beginFill(0x000000, 0.3);
		spTop.graphics.drawRect(0, 0, 5, 10);
		spTop.graphics.endFill();
		spTop.name = TOP_ARROW;
		
		var spBg:Sprite = new Sprite();
		spBg.graphics.beginFill(0x000000, 0.3);
		spBg.graphics.drawRect(0, 0, 5, 30);
		spBg.graphics.endFill();
		spBg.y = spTop.height;
		spBg.name = BG;
		
		var spBottom:Sprite = new Sprite();
		spBottom.graphics.beginFill(0x000000, 0.3);
		spBottom.graphics.drawRect(0, 0, 5, 10);
		spBottom.graphics.endFill();
		spBottom.y = spBg.y + spBg.height;
		spBottom.name = BOTTOM_ARROW;
		
		sp.addChild(spTop);
		sp.addChild(spBg);
		sp.addChild(spBottom);
		
        bgAsset = sp;
        this.addChild(bgAsset);
        setSkin();
    }

    protected function setSkin():void
    {
        this.topArrow = bgAsset.getChildByName(TOP_ARROW);
        this.bottomArrow = bgAsset.getChildByName(BOTTOM_ARROW);
        this.bg = bgAsset.getChildByName(BG);
        
        this.width = Math.ceil(this.width);
    }
}
}